﻿using MvcBoost.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace System.Data
{
    public class DbContextServiceFactory<TContext> where TContext : DbContext
    {
        protected TContext Context { get; private set; }

        public DbContextServiceFactory(TContext context)
        {
            if (context == null) throw new ArgumentNullException("context");
            this.Context = context;
        }

        public TService GetService<TService>() where TService : DbContextService<TContext>, new()
        {
            // The fact that service supports a constructor with no parameters lets 
            TService service = new TService();
            if (service.ContextManager is IDelegatedDbContextManager<TContext>)
            {
                (service.ContextManager as IDelegatedDbContextManager<TContext>).SetContext(Context);        
            }
            return service;
        }
    }
}
